#include <wx/wx.h>
#include <wx/event.h>
#include <wx/stattext.h>

const int ID_PLUS = 101;
const int ID_MINUS = 102;

////////////////////////////////////////////////////////////////////////////
class LeftPanel : public wxPanel
{
public:
    LeftPanel(wxPanel *parent);

    void OnPlus(wxCommandEvent & event);
    void OnMinus(wxCommandEvent & event);

    wxButton *m_plus;
    wxButton *m_minus;
    wxPanel *m_parent;
    int count;

};

LeftPanel::LeftPanel(wxPanel * parent)
       : wxPanel(parent, -1, wxPoint(-1, -1), wxSize(-1, -1), wxBORDER_SUNKEN)
{
  count = 0;
  m_parent = parent;
  m_plus = new wxButton(this, ID_PLUS, wxT("+"), 
      wxPoint(10, 10));
  m_minus = new wxButton(this, ID_MINUS, wxT("-"), 
      wxPoint(10, 60));
  Connect(ID_PLUS, wxEVT_COMMAND_BUTTON_CLICKED, 
      wxCommandEventHandler(LeftPanel::OnPlus));
  Connect(ID_MINUS, wxEVT_COMMAND_BUTTON_CLICKED, 
      wxCommandEventHandler(LeftPanel::OnMinus));
}

void LeftPanel::OnPlus(wxCommandEvent & event)
{
  count++;
//   Communicate *comm = (Communicate *) m_parent->GetParent();
//   comm->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"), count));
  event.Skip();
}

void LeftPanel::OnMinus(wxCommandEvent &event)
{
  count--;

//   Communicate *comm = (Communicate *) m_parent->GetParent();
//   comm->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"), count));

    event.Skip();
}

//////////////////////////////////////////////////////////////////////////////
class RightPanel : public wxPanel
{
public:
    RightPanel(wxPanel *parent);

    void OnSetText(wxCommandEvent & event);

    wxStaticText *m_text;
    int count = 0;

};

RightPanel::RightPanel(wxPanel * parent)
       : wxPanel(parent, wxID_ANY, wxDefaultPosition, 
         wxSize(270, 150), wxBORDER_SUNKEN)
{
    m_text = new wxStaticText(this, -1, wxT("0"), wxPoint(40, 60));
}

void RightPanel::OnSetText(wxCommandEvent &event)
{
    if(event.GetId() == ID_PLUS)
    {
        m_text->SetLabel(wxString::Format(wxT("%d"), ++count));
    }
    else if (event.GetId() == ID_MINUS)
    {
        m_text->SetLabel(wxString::Format(wxT("%d"), --count));
    }
}

//////////////////////////////////////////////////////////////////////////////

class Communicate : public wxFrame
{
public:
    Communicate(const wxString& title);

    LeftPanel *m_lp;
    RightPanel *m_rp;
    wxPanel *m_parent;

};
Communicate::Communicate(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(290, 150))
{
  m_parent = new wxPanel(this, wxID_ANY);

  wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);

  m_lp = new LeftPanel(m_parent);
  m_rp = new RightPanel(m_parent);

  hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5);
  hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5);

  m_parent->SetSizer(hbox);

  this->Centre();

  Bind(wxEVT_COMMAND_BUTTON_CLICKED, &RightPanel::OnSetText, m_rp, ID_PLUS,ID_MINUS);
}

/////////////////////////////////////////////////////////////////////////////
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

bool MyApp::OnInit()
{
    Communicate *frame = new Communicate(wxT("Communication"));
    frame->Show(true);

    return true;
}

wxIMPLEMENT_APP(MyApp);